
set more off

global data ../china/data
use $data/all0506_hs10, clear

local hs10list 8521900000 8525209070 8520900080 8471500085 9405106010 7321116000 8528215501 8414513000 ///
8471604580 6403996040 9401790005 8525404000 8471801000 6403999031 4011201015 8708704545 8467210010 ///
6403916040 3925301000 8528127201 8415103040 6402999030 8472909080 8519990045 8467220070 6402999060 ///
8414519090 7202700000 3926201010 8509100080 6302319020 6110121060 9401308030 8516290030 8712003500 ///
8539310060 8708395030 6111206010 8527136040 8527316040 8516710020 6402991815 9401790015 6306229030 ///
8471704065 8471300000 6204699044 6307909889 6402991865 9405408000 

gen samp=0 

foreach num of local hs10list {

replace samp=1 if hs=="`num'"
}

egen min_year=min(year)
drop if year!=min_year

gen samp_v=samp*v

egen tot_tot_v=total(v)
egen tot_samp_v=total(samp_v)
disp tot_samp_v/tot_tot_v

drop tot_tot_v tot_samp_v

qui {
drop if samp==0

bysort hs: egen ind_v=total(v)
egen tot_v=total(v)

gen trade_share=ind_v/tot_v

duplicates drop hs, force

gen i=0

foreach num of local hs10list {
cap drop i_max
egen i_max=max(i)
replace i=1+i_max if hs=="`num'"
}
}
sort i

outfile trade_share using $data/trade_share.raw, replace

/*keep trade_share*/
save $data/trade_share.dta, replace
